package net.zoneland.knowledge.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;

/**
 * .
 *
 * @author: bcc
 * 2020/9/15
 */
@Configuration
@ConfigurationProperties(prefix = "uom.datasource")
@MapperScan(basePackages = UomMybatisConfig.PACKAGE, sqlSessionFactoryRef = "uomSqlSessionFactory")
public class UomMybatisConfig {

    /**
     * dao扫描路径.
     */
    static final String PACKAGE = "net.zoneland.knowledge.uom.dao";
    /**
     * 类型.
     */
    private String type;
    /**
     * 数据库属性配置项.
     */
    private DataSourcePropsVO druid;

    /**
     * 数据源.
     * @return 数据源
     */
    @Bean("uomDataSource")
    public DataSource dataSource() {
        final DruidDataSource druidDataSource = new DruidDataSource();
        druid.initProperties(druidDataSource);
        return druidDataSource;
    }

    /**
     * hr的sqlSessionFactory.
     * @param dataSource 数据源
     * @return session工厂
     * @throws Exception 异常
     */
    @Bean("uomSqlSessionFactory")
    public SqlSessionFactory uomSessionFactory(@Qualifier("uomDataSource") final DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean.getObject();
    }

    /**
     * hr的查询template.
     * @param sessionFactory session工厂
     * @return 查询模板
     */
    @Bean("uomSqlSessionTemplate")
    public SqlSessionTemplate uomSqlSessionTemplate(@Qualifier("uomSqlSessionFactory") final SqlSessionFactory sessionFactory) {
        return new SqlSessionTemplate(sessionFactory);
    }

    /**
     * 事务控制器.
     * @param uomDataSource 数据源
     * @return 事务控制器
     */
    @Bean(name = "uomTransactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("uomDataSource") final DataSource uomDataSource) {
        return new DataSourceTransactionManager(uomDataSource);
    }

    /**
     * 获取 类型.
     * @return 类型
     */
    public String getType() {
        return type;
    }

    /**
     * 设置 类型.
     * @param type 类型
     */
    public void setType(final String type) {
        this.type = type;
    }
    /**
     * 获取 数据源配置.
     * @return 数据源配置
     */
    public DataSourcePropsVO getDruid() {
        return druid;
    }

    /**
     * 设置 数据源配置.
     * @param druid 数据源配置
     */
    public void setDruid(final DataSourcePropsVO druid) {
        this.druid = druid;
    }
}
